package com.atguigu.gulimall.product.app;

import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;
import java.util.concurrent.TimeUnit;


/**
 * 分布式锁
 *
 * @author leifengyang
 * @email leifengyang@gmail.com
 * @date 2019-10-01 22:50:32
 */
@RestController
public class IndexController1 {

    @Autowired
    RedissonClient redissonClient;

    @Autowired
    StringRedisTemplate redisTemplate;

    private static final String DOOR = "door";



    @GetMapping("/setLatch")
    @ResponseBody
    public String setLatch() throws InterruptedException {
        RCountDownLatch lock = redissonClient.getCountDownLatch(DOOR);
        lock.trySetCount(5);
        lock.wait();
        return "门栓被放开";

    }

    @GetMapping("/offLatch")
    @ResponseBody
    public String offLatch() {
        RCountDownLatch lock = redissonClient.getCountDownLatch(DOOR);
        lock.countDown();
        return "门栓被放开1";
    }



    /**
     * 信号量
     * @return
     */
    @GetMapping("/park")
    @ResponseBody
    public String park() {
        RSemaphore lock = redissonClient.getSemaphore("park");
        try {
            lock.acquire(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return "停车，占一个车位1,当前停车数量：";
    }

    @GetMapping("/go")
    @ResponseBody
    public String go() {
        RSemaphore lock = redissonClient.getSemaphore("park");
        lock.release(1);
        return "开走，放出一个车位1,当前停车数量：";
    }















    @GetMapping("/read")
    @ResponseBody
    public String read() {
        RReadWriteLock lock = redissonClient.getReadWriteLock("ReadWrite-Lock");
        RLock rLock = lock.readLock();
        String s = "";
        try {
            rLock.lock();
            System.out.println("读锁加锁"+Thread.currentThread().getId());
            Thread.sleep(5000);
            s= redisTemplate.opsForValue().get("lock-value");
        }finally {
            rLock.unlock();
            return "读取完成:"+s;
        }
    }

    @GetMapping("/write")
    @ResponseBody
    public String write() {
        RReadWriteLock lock = redissonClient.getReadWriteLock("ReadWrite-Lock");
        RLock wLock = lock.writeLock();
        String s = UUID.randomUUID().toString();
        try {
            wLock.lock();
            System.out.println("写锁加锁"+Thread.currentThread().getId());
            Thread.sleep(10000);
            redisTemplate.opsForValue().set("lock-value",s);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            wLock.unlock();
            return "写入完成:"+s;
        }
    }



    @GetMapping("/hello")
    public String hello() {
        RLock lock = redissonClient.getLock("my-lock");
        lock.lock();

        try {
            System.out.println("加锁成功，执行业务...." + Thread.currentThread().getId());
            TimeUnit.SECONDS.sleep(35);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            System.out.println("锁释放..." + Thread.currentThread().getId());
            lock.unlock();
        }
        return "hello";
    }

}
